﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Throw_In.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Throw_In
{
    public partial class Home : PhoneApplicationPage
    {
        public Home()
        {
            InitializeComponent();
            if (!App.start)
            {
                Thread.Sleep(2000);
                App.start = true;
            }
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string strItemIndex;

            if (NavigationContext.QueryString.TryGetValue("goto", out strItemIndex))
                panorama.DefaultItem = panorama.Items[Convert.ToInt32(strItemIndex)];

            if (!App.AllDataModel.IsDataLoaded)
            {
                App.AllDataModel.LoadData();
            }
            BookmarksBox.ItemsSource = App.AllDataModel.Bookmarks;
            RecentlyBox.ItemsSource = App.AllDataModel.RecentItems;
            System.Diagnostics.Debug.WriteLine(App.AllDataModel.InfoItems.Count);
            InfoBox.ItemsSource = App.AllDataModel.getRandomInfo();
            System.Diagnostics.Debug.WriteLine(App.AllDataModel.InfoItems.Count);

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //top news
            NavigationService.Navigate(new Uri("/GroupOne.xaml", UriKind.Relative));
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //random news
            NavigationService.Navigate(new Uri("/GroupOne.xaml?goto=1", UriKind.Relative));
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GroupTwo.xaml?goto=1", UriKind.Relative));
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GroupTwo.xaml?goto=2", UriKind.Relative));
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GroupTwo.xaml?goto=3", UriKind.Relative));

        }

        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GroupTwo.xaml?goto=4", UriKind.Relative));

        }
        private void Button_Click_7(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GroupTwo.xaml?goto=5", UriKind.Relative));

        }
        private void Button_Click_8(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GroupTwo.xaml?goto=6", UriKind.Relative));

        }
        private void Button_Click_9(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GroupTwo.xaml?goto=7", UriKind.Relative));

        }
        private void Button_Click_10(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GroupTwo.xaml?goto=8", UriKind.Relative));

        }

        private void AboutClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/About.xaml?", UriKind.Relative));
        }


        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {

        }
        private void ApplicationBarStateChanged(object sender, EventArgs e)
        {
        }
        private void BookmarksChange(object sender, EventArgs e)
        {
            if (BookmarksBox.SelectedIndex == -1)
                return;
            PhoneApplicationService.Current.State["Item"] = BookmarksBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
            // Reset selected index to -1 (no selection)
            BookmarksBox.SelectedIndex = -1;
        }
        private void RecentlyChange(object sender, EventArgs e)
        {
            if (RecentlyBox.SelectedIndex == -1)
                return;
            PhoneApplicationService.Current.State["Item"] = RecentlyBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
            // Reset selected index to -1 (no selection)
            RecentlyBox.SelectedIndex = -1;
        }
        private void InfoChange(object sender, EventArgs e)
        {
            if (InfoBox.SelectedIndex == -1)
                return;
            PhoneApplicationService.Current.State["Item"] = InfoBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
            // Reset selected index to -1 (no selection)
            InfoBox.SelectedIndex = -1;
        }
        private void searchClick(object sender, EventArgs e)
        {
            PhoneApplicationService.Current.State["Search"] = "something";
            NavigationService.Navigate(new Uri("/SearchResult.xaml", UriKind.Relative));
        }
    }
}